"""
    Created By Khun on 2020/4/7
"""

__author__ = 'Khun'

from flask_login import UserMixin
from sqlalchemy import Column, Integer, String, Boolean
from werkzeug.security import generate_password_hash, check_password_hash

from app.models.base import Base


class User(UserMixin, Base):
    id = Column(Integer, primary_key=True, autoincrement=True)
    username = Column(String(32), nullable=False, unique=True)
    nickname = Column(String(32), nullable=False)
    _password = Column('password', String(255))
    is_superuser = Column(Boolean, default=False)

    @property
    def password(self):
        return self._password

    @password.setter
    def password(self, raw):
        self._password = generate_password_hash(raw)

    def check_password(self, raw):
        if not self._password:
            return False
        return check_password_hash(self._password, raw)
